package com.centling;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadServlet extends HttpServlet {

	private String uploadFolder;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		
		/*
		 * FileItem 代表一个文件或者一个键值对
		 * FileItemFactory 创建fileItem的factory
		 * ServletFileUpload 用于处理request，并将request转化成一堆fileItem
		 */
		System.out.println("系统默认的临时保存文件："+System.getProperty("java.io.tmpdir"));
		String projectAbsolutePath = this.getServletContext().getRealPath("/");
		System.out.println("工程的绝对路径是："+projectAbsolutePath);
		
		String defaultEncoding = System.getProperty("file.encoding");
		System.out.println("系统默认的编码："+defaultEncoding);
		
		
		request.setCharacterEncoding("UTF-8");
		
		/*
		int thresHold = 0;
		File repository = null;
		*/
		
		FileItemFactory fileItemFactory = new DiskFileItemFactory();
		ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
		
		
		try {
			
			List<FileItem> fileItems = servletFileUpload.parseRequest(request);
			for(FileItem fileItem : fileItems){
				
				boolean formFieldFlag = fileItem.isFormField();
				
				if(formFieldFlag){//是form表单的键值对
					
					String fieldName = fileItem.getFieldName();
					//此句相当于request.getParameter("name")
					String fileValue = fileItem.getString("UTF-8");
					System.out.println("fieldName是："+fieldName+"---"+"fileValue是："+fileValue);
					
				}else{//是文件
					
					String fileName = fileItem.getName();
					System.out.println("文件的名字是： "+fileName);
					
					String parentPath = projectAbsolutePath+uploadFolder;
					System.out.println("上传文件的存储目录是:"+parentPath);
					
					
					File parent = new File(parentPath);
					if(parent.exists()){
						System.out.println("上传文件的存储目录已经存在");
					}else{
						System.out.println("上传文件的存储目录不存在");
						parent.mkdir();
					}
					File targetFile = new File(parent, fileName);
					fileItem.write(targetFile);
					/**/
				}
			}
			
		} catch (FileUploadException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		System.out.println("执行文件上传的init有参方法了");
		uploadFolder = config.getInitParameter("uploadfilefolder");
		super.init(config);
	}

	@Override
	public void init() throws ServletException {
		System.out.println("执行文件的init无参方法执行了");
		super.init();
	}

	
	
	
}
